<?php

namespace App\Models;


class BucketData extends BaseModel
{
    protected $table = "bucket_data";


    protected $rules = [
        'admin_id' => 'required',
        'shop_id' => 'required',
        'bucket_num_in' => 'required|numeric',
        'bucket_num_out' => 'required|numeric',
        'bucket_category_id' => 'required|numeric|unique:bucket_data,bucket_category_id',
    ];

    protected $message = [
        'admin_id.required' => '用户ID获取失败',
        'shop_id.required' => '店铺ID获取失败',
//        'shop_name.required' => '商品名称必须填写',
//        'shop_name.between' => '商品名称不能超过20个字',
        'bucket_num_in.required' => '店内水桶数量必须填写',
        'bucket_num_in.numeric' => '店内总数量必须是纯数字',
        'bucket_num_out.required' => '店外数量必须填写',
        'bucket_num_out.numeric' => '店外数量必须是纯数字',
        'bucket_category_id.required' => '请选择所属水桶品牌',
        'bucket_category_id.numeric' => '非法水桶品牌数据',
        'bucket_category_id.unique' => '该该品牌水桶数据已存在，请重新选择',
    ];

    public function category()
    {
        return $this->belongsTo('App\Models\BucketCategory', 'bucket_category_id', 'id');
    }

    public function shop()
    {
        return $this->belongsTo('App\Models\Shop', 'shop_id', 'id');
    }

    public function getDataList()
    {
        $where['admin_id'] = plat_admin_id();
        $where['shop_id'] = request('shop_id');
        request('bucket_category_id') && $where['bucket_category_id'] = request('bucket_category_id');
        request('keyword') && $where['shop_name'] = ['like', '%' . request('keyword') . '%'];
        $rows = $this->_getPageList($where);
        return $rows;
    }

    public function getOwnShopDataList($shopId)
    {
        $keyword = request('keyword');
        $where['admin_id'] = plat_admin_id();
        if ($shopId) {
            $where['shop_id'] = $shopId;
        }
        $condition = [];
        if ($keyword) {
            $condition['shop_name'] = ['like', '%' . $keyword . '%'];
        }
        $rows = $this->_getJoinPageList('shop',$where,'*',[],10,5,'id,shop_name',$condition);
        return $rows;
    }
    public function getLogListByShopId($shopId)
    {
        $keyword = request('keyword');
        $where['shop_id'] = $shopId;
        $where['status'] = ['>',0];
        if (is_numeric($keyword)) {
            $where['id'] = $keyword;
        } else {
            $where['goods_name'] = ['like', '%' . $keyword . '%'];
        }
        $rows = $this->_getPageList($where,'id,goods_name,goods_num,goods_price');
        return $rows;
    }

    public function addOrUpdate($shopId)
    {
        $data['admin_id'] = plat_admin_id();
        $data['shop_id'] = $shopId;
//        $data['shop_name'] = trim(request('shop_name'));
        $data['bucket_num_in'] = intval(trim(request('bucket_num_in',0)));
        $data['bucket_num_out'] = intval(trim(request('bucket_num_out',0)));
        $data['bucket_category_id'] = request('bucket_category_id');
        if (request('id')) {
            $data['id'] = trim(request('id'));
            $this->rules['bucket_category_id'] .= ',' . $data['id'] . ',id,' . 'shop_id,' . $data['shop_id']; //唯一字段验证,在更新时排除当前店铺中的当前商品id
        } else {
            $this->rules['bucket_category_id'] .= ',' . NULL . ','.NULL  .',shop_id,' . $data['shop_id']; //唯一字段验证,在更新时排除当前店铺中的当前商品id
        }
        if (!$this->_validate($data)) {
            return false;
        }
        $res = $this->_save($data);
        if (!$res) {
            $this->error = '保存商品信息失败！';
            return false;
        }
        //提交事务
        return $res;
    }

    public function getResultByData($categories)
    {
        if(count($categories)) {
            $ids = [];
            foreach($categories as $cate){
                $ids[] = $cate['id'];
            }
            $data = $this->_getList(['bucket_category_id'=>['in', $ids]]);
            foreach($categories as $key=>$val) {
                $categories[$key]['bucket_num_out'] = 0;
                foreach($data as $item){
                    if($val['id'] == $item['bucket_category_id']){
                        $categories[$key]['bucket_num_out'] = $item['bucket_num_out'];
                    }
                }
            }
        }

        return $categories;
    }

}
